﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Code.Models;
using Company.Web.Areas.Manage.Models.MessageModels;
using Code.BLL;
using Company.Web.Commen;

namespace Company.Web.Areas.Manage.Controllers
{
    public class MessageController : ControllerBase
    {
        MessageManage MM = new MessageManage();
        public ActionResult Index(IndexModel model)
        {
            if (model == null)
                model = new IndexModel();
            if (model.Serch == null)
                model.Serch = new IndexSerchModel();
            if (model.ID < 1)
                model.ID = null;
            if (model.A < 1)
                model.A = null;
            var page = model.ID ?? 1;
            model.Size = model.A ?? 10;
            model.Languages = (Enum.GetValues(typeof(Language)) as Language[]).Select(p => new SelectListItem { Value = p.ToString(), Text = p.Name() }).ToList();
            model.Languages.Insert(0, new SelectListItem { Text = "所有", Value = "" });
            model.States = (Enum.GetValues(typeof(MessageState)) as MessageState[]).Select(p => new SelectListItem { Value = p.ToString(), Text = p.Name() }).ToList();
            model.States.Insert(0, new SelectListItem { Text = "所有", Value = "" });

            var data = MM.GetTable<Message>();
            if (!string.IsNullOrEmpty(model.Serch.Name))
                data = data.Where(p => p.Name.Contains(model.Serch.Name));
            if (!string.IsNullOrEmpty(model.Serch.Title))
                data = data.Where(p => p.Title.Contains(model.Serch.Title));
            if (model.Serch.StartTime.HasValue)
                data = data.Where(p => p.AddTime >= model.Serch.StartTime.Value.Date);
            if (model.Serch.EndTime.HasValue)
                data = data.Where(p => p.AddTime < model.Serch.EndTime.Value.AddDays(1).Date);
            if (model.Serch.Language.HasValue)
                data = data.Where(p => p.Language == (short)model.Serch.Language);
            if (model.Serch.State.HasValue)
                data = data.Where(p => p.State == (short)model.Serch.State);
            model.SumCount = data.Count();
            model.Msgs = data.OrderByDescending(p => p.AddTime)
                .Skip(model.Size * (page - 1)).Take(model.Size)
                .Select(p => new MessageItem
                {
                    ID = p.ID,
                    Name = p.Name,
                    Title = p.Title,
                    ContactInfo = p.ContactInfo,
                    AddTime = p.AddTime,
                    Language = (Language)p.Language,
                    State = (MessageState)p.State,
                }).ToArray();
            return View(model);
        }

        public ActionResult Detail(long id )
        {
            var data = MM.Read(id);
            if (data == null)
                return Content(Url.Action("index").Redirect("不存在该留言"));
            var model = new MessageItem
            {
                ID = data.ID,
                Name = data.Name,
                Title = data.Title,
                ContactInfo = data.ContactInfo,
                Content = data.Content,
                AddTime = data.AddTime,
                Language = (Language)data.Language,
                State = (MessageState)data.State,
            };



            if (Request.IsAjaxRequest())
                return PartialView("Detail_Ascx", model);
            return View(model);
        }

        [ActionName("Index")]
        [MultiButton("Del")]
        public ActionResult Del(long[] MsgID, IndexModel model)
        {
            string msg = null;
            if (MsgID == null || MsgID.Length == 0)
            {
                msg = "请先选中要删除的项！";
            }
            else
            {
                int rs = MM.Del(MsgID);
                if (rs > 0)
                    msg = "成功删除" + rs + "条";
                else
                    msg = "操作失败，请稍后再试！";
            }
            if ("GET".Equals(Request.HttpMethod, StringComparison.CurrentCultureIgnoreCase))
                return Content(Url.Action("index").Redirect(msg));
            ViewData["msg"] = msg.AlertOffPage();
            return Index(model);
        }

    }
}
